package com.jwthhealth_pub.splash;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.widget.LinearLayout;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.app.App;
import com.jwthhealth_pub.app.view.MainActivity;
import com.jwthhealth_pub.common.preference.PreferenceKey;
import com.jwthhealth_pub.old.sign.view.SignInActivity;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * splah页面不应初始化太多参数？
 * Created by liuzhen000 on 2016/11/22.
 */

public class SplashActivity extends Activity {

    @BindView(R.id.root_layout)
    LinearLayout layoutRoot;
    private Handler handler = new Handler();

    private boolean isFristLogin = false;
    // TODO: 2016/12/6 在Client/User类中存储登陆状态
    private boolean cacheToken;
    private boolean isLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_splash);
        ButterKnife.bind(this);

        WindowManager wm = (WindowManager) this
                .getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        App.preferenceUtil.putInt(PreferenceKey.SCREEN_WIDTH, width);
//        int height = wm.getDefaultDisplay().getHeight();

        AlphaAnimation alphaAnimation = new AlphaAnimation(.3f, 1f);
        alphaAnimation.setDuration(1500);
        layoutRoot.setAnimation(alphaAnimation);

        gotoNext();

        //android 6.0 权限问题
//        if (Build.VERSION.SDK_INT >= 23 && (!(checkSelfPermission(Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED))){
//            //andorid 6.0权限问题
//            String[] permissions = new String[]{Manifest.permission.READ_PHONE_STATE};
//            requestPermissions(permissions, App.READ_PHONE_STATE);
//        } else {
//            gotoNext();
//        }
    }

    @Override
    protected void onStart() {
        super.onStart();

    }

    private void gotoNext(){
        new Thread() {

            @Override
            public void run() {
                super.run();
//                isFristLogin = App.preferenceUtil.getBoolean(PreferenceKey.FIRST_LOGIN, true);
                isLogin = App.preferenceUtil.getBoolean(PreferenceKey.CACHE_TOKEN, false);

                handler.postDelayed(new Runnable() {

                    @Override
                    public void run() {
//                        if (isFristLogin) {
                        if (isLogin) {
//                            gotoGuide();
                            gotoHome();
                        } else {
//                            cacheToken = App.preferenceUtil.getBoolean(PreferenceKey.CACHE_TOKEN, false);
//                            gotoGuide();
//                            gotoHome();
//                            gotoTest(9);
//                            if (cacheToken) {
//                                gotoHome();
//                            } else {
                                gotoLogin();
//                            }
                        }
                    }
                }, 2000);
            }
        }.start();
    }

    private void gotoLogin() {
        startActivity(new Intent(this, SignInActivity.class));
        finish();
    }

    private void gotoHome() {
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }

//    private void gotoGuide() {
//        startActivity(new Intent(this, GuideActivity.class));
//        finish();
//    }

//    private void gotoTest(){
//        startActivity(new Intent(this, ServiceTermActivity.class));
//        finish();
//    }

    /**
     * 权限获取结果回调
     * @param requestCode tag
     * @param permissions 权限id
     * @param grantResults 结果
     */
//    @Override
//    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
//        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//        if (requestCode == App.READ_PHONE_STATE) {
//            for (int i=0; i<permissions.length; i++){
//                if (permissions[i].equals(Manifest.permission.READ_PHONE_STATE)) {
//                    if (grantResults[i] == 0){
//                        Log.d("SplashActivity", "已经获取权限");
//                        gotoNext();
//                    } else {
//                        Toast.makeText(this, "拒绝权限程序将无法执行", Toast.LENGTH_SHORT).show();
//                    }
//                }
//            }
//        }
//    }


}
